package org.csu.jpetstore.web.servlet;

import com.alibaba.fastjson.JSON;
import org.csu.jpetstore.service.AccountService;
import org.csu.jpetstore.util.Result;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class UsernameExistServlet extends HttpServlet {

    AccountService accountService = new AccountService();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");

        //response.setContentType("text/plain");
        response.setContentType("text/json");
        PrintWriter out = response.getWriter();

        Result result1 = new Result();

        if(!accountService.usernameExist(username)) {
            //out.print("Not Exist");
            result1.setCode(0);
            result1.setMsg("Not Exist");
        }
        else {
            //out.print("Exist");
            result1.setCode(1);
            result1.setMsg("Exist");
        }

        String str = JSON.toJSONString(result1);
        out.print(str);

        out.flush();
        out.close();
    }
}
